package com.zhanglifan.action;

import com.opensymphony.xwork2.ModelDriven;
import com.zhanglifan.domain.Contract;
import com.zhanglifan.domain.User;
import com.zhanglifan.service.ContractService;
import com.zhanglifan.util.CstUtils;
import com.zhanglifan.util.Page;
import com.zhanglifan.util.SysConstant;
import org.apache.struts2.ServletActionContext;

/**
 * Information
 * Author: ZhangLifan
 * Time: 2017/10/6 8:41
 * Description: ContractAction
 */
public class ContractAction extends BaseAction implements ModelDriven<Contract>{
    private Contract contract = new Contract();
    private ContractService contractService;
    private Page<Contract> page = new Page<>();

    public String list(){
        User user = (User) session.get(SysConstant.CURRENT_USER_INFO);
        page.setUrl("contractAction_list");

        String id = null;
        String hql = "from Contract where 1=1 ";
        if(user.getUserinfo().getDegree()==3){
            hql += "and createDept=? ";
            id = user.getDept().getId();
        }else if(user.getUserinfo().getDegree()==4){
            hql += "and createBy=? ";
            id = user.getId();
        }
        hql += "order by createTime desc,id";
        contractService.findPage(hql, page, Contract.class, id==null ? null :new String[]{id});
        super.push(page);
        return "listSuccess";
    }

    public String toview(){
        super.push(contractService.get(Contract.class,contract.getId()));
        return "viewSuccess";
    }

    public String tocreate(){

        return "toCreateSuccess";
    }

    public String insert(){
        User user = (User) session.get(SysConstant.CURRENT_USER_INFO);
        contract.setState(0);
        contract.setTotalAmount(0.0);
        contract.setCreateBy(user.getId());
        contract.setCreateDept(user.getDept().getId());
        contractService.saveOrUpdate(contract);
        return "insertSuccess";

    }

    public String update(){
        Contract c = contractService.get(Contract.class,contract.getId());
        c.setCustomName(contract.getCustomName());
        c.setPrintStyle(contract.getPrintStyle());
        c.setContractNo(contract.getContractNo());
        c.setOfferor(contract.getOfferor());
        c.setInputBy(contract.getInputBy());
        c.setCheckBy(contract.getCheckBy());
        c.setInspector(contract.getInspector());
        c.setSigningDate(contract.getSigningDate());
        c.setImportNum(contract.getImportNum());
        c.setShipTime(contract.getShipTime());
        c.setTradeTerms(contract.getTradeTerms());
        c.setDeliveryPeriod(contract.getDeliveryPeriod());
        c.setCrequest(contract.getCrequest());
        c.setRemark(contract.getRemark());
        contractService.saveOrUpdate(c);
        return "updateSuccess";
    }

    public String toupdate(){
        super.push(contractService.get(Contract.class,contract.getId()));
        return "toUpdateSuccess";
    }

    public String delete(){

        String[] ids = contract.getId().split(", ");

        contractService.delete(Contract.class,ids);

        return "deleteSuccess";
    }

    public String submit(){
        String[] ids = contract.getId().split(", ");
        CstUtils.changeStatus(contractService,Contract.class,ids,1);
        return list();
    }

    public String cancel(){
        String[] ids = contract.getId().split(", ");
        CstUtils.changeStatus(contractService,Contract.class,ids,0);
        return list();
    }

    public String print() throws Exception {
        new ContractPrint().print(contractService.get(Contract.class,contract.getId()), ServletActionContext.getServletContext().getRealPath("/"),ServletActionContext.getResponse());
        return NONE;
    }

    public void setContractService(ContractService contractService) {
        this.contractService = contractService;
    }

    @Override
    public Contract getModel() {
        return contract;
    }

    public Page<Contract> getPage() {
        return page;
    }

}
